package com.steamedfish.auctioncloudadserver.feign.service;


import com.steamedfish.auctioncloudadserver.model.AuctionItemModel;
import com.steamedfish.auctioncloudadserver.service.AdMailService;
import com.steamedfish.auctioncloudadserver.service.elasticsearch.ElasticSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/auctioncloudadserver/service/adMailService")
public class AdMailRestService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AdMailService adMailService;

    @Autowired
    private ElasticSearchService elasticSearchService;

    @RequestMapping(value = "/sendalertinfo")
    public ResponseEntity<?> sendAlertMail(@RequestParam String id,
                                           @RequestParam String[] mails
                                           ){
        AuctionItemModel auctionItemModel = elasticSearchService.getAuctionItemModel(id);
        String context = adMailService.getAlertContext(auctionItemModel);
        String subject = "蒸鱼校园拍买平台商品开拍提醒";
        List<String> mailsList = Arrays.asList(mails);

        adMailService.sendMail(mailsList, subject, context);
        return ResponseEntity.ok("sucess");
    }

    @RequestMapping(value = "/sendresultmail")
    public ResponseEntity<?> sendResultMail(@RequestParam String context,
                                            @RequestParam String subject,
                                            @RequestParam String[] mails) {

        logger.info("蒸鱼校园拍买平台商品结算");
        List<String> mailsList = Arrays.asList(mails);
        adMailService.sendMail(mailsList, subject, context);

        return ResponseEntity.ok("success");
    }

}